//
//  NYOPMovieListViewController.m
//  Kokozu
//
//  Created by Bismarrck on 11-7-12.
//  Copyright 2011年 Tsinghua University. All rights reserved.
//

#import "NYOPMovieListViewController.h"
#import "MovieDetailViewController.h"
#import "NYOPMovieDetailViewController.h"
#import "NYOPMovieListCell.h"
#import "KKZBaseCell.h"



@implementation NYOPMovieListViewController

@synthesize NYOPMovieRecords;

- (void)dealloc
{
    [NYOPMovieRecords release];
    
    [super dealloc];
}

- (void)setupMovieRecords
{
    // Set the NYOPMovieRecords
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:6];
    for (int i = 0; i < 6; i++) {
        NSLog(@"%d",i);
        NYOPMovieRecord *aRecord;
        aRecord = [[NYOPMovieRecord alloc] initWithIndex:i];
        [array addObject:aRecord];
        [aRecord release];
    }
    
    self.NYOPMovieRecords = array;
    [array release];
}

- (void)viewDidLoad
{
    [self setupMovieRecords];
    [self addTitleToNavigationBar:@"讨价还价"];
    
    // Set the rows count
    if (self.NYOPMovieRecords) {
        rowsCount_ = [self.NYOPMovieRecords count] + 1;
    }
    else {
        rowsCount_ = 1;
    }
    
    // Call super to load
    [super viewDidLoad];
}

#pragma mark - 
#pragma mark - Table View Data Source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == rowsCount_ - 1) {
        
        if (tableFooterCell_ == nil) {
            tableFooterCell_ = [[TableFooterCell alloc] initWithFrame:TableFooterFrame BackgroundColor:self.view.backgroundColor delegate:self];
        }
        tableFooterCell_.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return tableFooterCell_;
    }
    else {
        
        static NSString *NormalCellIdentifier = @"NormalCellIdentifier";
        static NSString *ExpandCellIdentifier = @"ExpandCellIdentifier";
        
        KKZBaseCell *cell;
        
        if (selectedIndexPath_ != indexPath) {
            
            cell = (NYOPMovieListCell *)[tableView dequeueReusableCellWithIdentifier:NormalCellIdentifier];
            
            if (cell == nil) {
                [self.normalCellNib instantiateWithOwner:self options:nil];
                cell = self.normalTmpCell;
                self.normalTmpCell = nil;
            }
            
            [self configureCell:cell atIndexPath:indexPath options:NORMAL_CELL];
            
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        else if (selectedIndexPath_ == indexPath && cellIsExpanded == YES) {
            
            cell = (NYOPMovieListCell *)[tableView dequeueReusableCellWithIdentifier:NormalCellIdentifier];
            
            if (cell == nil) {
                [self.normalCellNib instantiateWithOwner:self options:nil];
                cell = self.normalTmpCell;
                self.normalTmpCell = nil;
            }
            
            [self configureCell:cell atIndexPath:indexPath options:NORMAL_CELL];
            
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        else {
            
            cell = (NYOPMovieListCellExpanded *)[tableView dequeueReusableCellWithIdentifier:ExpandCellIdentifier];
            
            if (cell == nil) {
                [self.expandCellNib instantiateWithOwner:self options:nil];
                cell = self.expandTmpCell;
                self.expandTmpCell = nil;
            }
            
            [self configureCell:cell atIndexPath:indexPath options:EXPAND_CELL];
            
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
}


#pragma mark -
#pragma mark - Methods from super class

- (MovieDetailViewController *)nextViewControllerForIndexPath:(NSIndexPath *)indexPath
{
    return [[[NYOPMovieDetailViewController alloc] initWithMovieRecord:[self.NYOPMovieRecords objectAtIndex:indexPath.row]] autorelease];
}

- (UINib *)normalCellNib
{
    return [UINib nibWithNibName:@"NYOPMovieListCell" bundle:nil];
}

- (UINib *)expandCellNib
{
    return [UINib nibWithNibName:@"NYOPMovieListCellExpanded" bundle:nil];
}

- (CGFloat)normalCellHeight
{
    return 120.0f;
}

- (CGFloat)expandCellHeight
{
    return 300.0f;
}

- (void)configureCell:(KKZBaseCell *)cell atIndexPath:(NSIndexPath *)indexPath options:(CellState)state
{
    NYOPMovieRecord *aRecord = [self.NYOPMovieRecords objectAtIndex:indexPath.row];
    
    if (state == NORMAL_CELL) {
        cell.moviePoster = [aRecord moviePoster];
    }
    else {
        cell.movieFullPoster = [aRecord movieFullPoster];
    }
    
    cell.movieName = [aRecord movieName];
    cell.movieAvgPrice = [aRecord movieAvgPrice];
    cell.backgroundColor = self.view.backgroundColor;
    
    UIButton *accessoryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [accessoryButton setTag:indexPath.row];
    [accessoryButton setFrame:CGRectMake(243, 76, 60, 25)];
    [accessoryButton.titleLabel setFont:[UIFont systemFontOfSize:14.0f]];
    [accessoryButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [accessoryButton setTitle:@"就看它" forState:UIControlStateNormal];
    [accessoryButton setBackgroundImage:[UIImage imageNamed:@"ticket_button.png"] forState:UIControlStateNormal];
    [accessoryButton addTarget:self action:@selector(accessoryButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    [cell addSubview:accessoryButton];
}

@end
